package com.aspose.cells;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.security.KeyFactory;
import java.security.ProtectionDomain;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.RSAPublicKeySpec;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class License {
   private String[] b;
   private int c;
   private String d;
   private Date e;
   private Date f;
   private String g;
   static License a = null;

   public static boolean isLicenseSet() {
      return a != null;
   }

   public static Date getSubscriptionExpireDate() {
      return a == null ? null : a.a();
   }

   public void setLicense(InputStream stream) {
      Document var2 = null;
      if (stream != null) {
         try {
            DocumentBuilderFactory var3 = DocumentBuilderFactory.newInstance();
            DocumentBuilder var4 = var3.newDocumentBuilder();
            var2 = var4.parse(stream);
         } catch (Exception var5) {
            throw new CellsException(9, "Error when parse license stream.", var5);
         }
      }

      this.a(var2);
   }

   public void setLicense(String licenseFileName) {
      File var2 = this.a(licenseFileName);
      if (var2 == null) {
         throw new CellsException(9, "Cannot find license file: " + licenseFileName);
      } else {
         if (com.aspose.cells.a.c.zl.a()) {
            com.aspose.cells.a.c.zl.a("Using license file: " + var2.getAbsolutePath());
         }

         Document var3 = null;

         try {
            DocumentBuilderFactory var4 = DocumentBuilderFactory.newInstance();
            DocumentBuilder var5 = var4.newDocumentBuilder();
            var3 = var5.parse(var2);
         } catch (CellsException var6) {
            throw var6;
         } catch (Exception var7) {
            throw new CellsException(9, "Error when parse license file.", var7);
         }

         this.a(var3);
      }
   }

   private File a(String var1) {
      File var2 = new File(var1);
      if (var2.isFile()) {
         return var2;
      } else {
         if (com.aspose.cells.a.c.zl.a()) {
            com.aspose.cells.a.c.zl.a("Cannot find license file " + var2.getAbsolutePath());
         }

         try {
            HashMap var3 = new HashMap();
            var2 = this.a(License.class, var1, var3);
            if (var2 != null) {
               return var2;
            }

            StackTraceElement[] var4 = (new Exception()).getStackTrace();
            HashMap var5 = new HashMap();
            var5.put(License.class.getName(), Boolean.FALSE);

            for(int var6 = 2; var6 < var4.length; ++var6) {
               String var7 = var4[var6].getClassName();
               if (!var5.containsKey(var7)) {
                  var2 = this.a(Class.forName(var7), var1, var3);
                  if (var2 != null) {
                     return var2;
                  }

                  var5.put(var7, Boolean.FALSE);
               }
            }
         } catch (Throwable var8) {
            if (com.aspose.cells.a.c.zl.a()) {
               com.aspose.cells.a.c.zl.a(var8.getClass().getName() + ":" + var8.getMessage());
            }
         }

         return null;
      }
   }

   private File a(Class var1, String var2, HashMap<String, Boolean> var3) throws Exception {
      ProtectionDomain var4 = var1.getProtectionDomain();
      if (var4 != null) {
         CodeSource var5 = var4.getCodeSource();
         if (var5 != null) {
            File var6 = new File(URLDecoder.decode(var5.getLocation().getFile(), "UTF-8"));
            if (var6.getName().endsWith(".jar")) {
               var6 = var6.getParentFile();
            }

            String var7 = var6.getCanonicalPath();
            if (var3.containsKey(var7)) {
               return null;
            }

            var6 = new File(var6, var2);
            if (var6.isFile()) {
               return var6;
            }

            var3.put(var7, Boolean.FALSE);
            if (com.aspose.cells.a.c.zl.a()) {
               com.aspose.cells.a.c.zl.a("Cannot find license file " + var6.getAbsolutePath());
            }
         }
      }

      return null;
   }

   public void setLicense(Reader reader) {
      Document var2 = null;
      if (reader != null) {
         try {
            DocumentBuilderFactory var3 = DocumentBuilderFactory.newInstance();
            DocumentBuilder var4 = var3.newDocumentBuilder();
            var2 = var4.parse(new InputSource(reader));
         } catch (Exception var5) {
            throw new CellsException(9, "Error when parse license reader.", var5);
         }
      }

      this.a(var2);
   }

   private void a(Document var1) {
      a = this;
      zayi.a();
   }

   Date a() {
      return this.e;
   }

   Date b() {
      return this.f;
   }

   private static String b(String var0) {
      if ("Cells".equals("Cells")) {
         return d(var0);
      } else if ("Cells".equals("Android")) {
         return c(var0);
      } else {
         return "Cells".equals("Slides") ? e(var0) : d(var0);
      }
   }

   private static String c(String var0) {
      if (!var0.equals("Aspose.Total for Android") && !var0.equals("Aspose.Cells for Android")) {
         throw new CellsException(9, "The license is not valid for this product.");
      } else {
         return "Aspose.Cells for Java";
      }
   }

   private static String d(String var0) {
      if (var0.equals("Aspose.Excel for Java")) {
         return "Aspose.Cells for Java";
      } else {
         return !var0.equals("Aspose.Total") && !var0.equals("Aspose.Total for Java") && !var0.equals("Aspose.Total Product Family") && !var0.equals("Aspose.Cells Product Family") ? var0 : "Aspose.Cells for Java";
      }
   }

   private static String e(String var0) {
      if (!var0.equals("Aspose.Total") && !var0.equals("Aspose.Total for Java") && !var0.equals("Aspose.Slides for Java") && !var0.equals("Aspose.Total Product Family") && !var0.equals("Aspose.Slides Product Family")) {
         throw new CellsException(9, "The license is not valid for this product.");
      } else {
         return "Aspose.Cells for Java";
      }
   }

   private void a(Node var1) {
      try {
         Node var2 = zbvu.a(var1.getChildNodes(), "Products");
         NodeList var3 = var2 != null ? var2.getChildNodes() : null;
         if (var3 != null) {
            this.b = new String[var3.getLength()];

            for(int var4 = 0; var4 < var3.getLength(); ++var4) {
               this.b[var4] = b(b(var3.item(var4).getLastChild()));
            }
         }

         Node var11 = zbvu.a(var1.getChildNodes(), "EditionType");
         if (var11 != null) {
            this.c = g(b(var11.getLastChild()));
         }

         Node var5 = zbvu.a(var1.getChildNodes(), "SerialNumber");
         if (var5 != null) {
            this.d = b(var5.getLastChild());
         } else {
            this.d = "";
         }

         Node var6 = zbvu.a(var1.getChildNodes(), "SubscriptionExpiry");
         if (var6 != null) {
            this.e = c(var6.getLastChild());
         }

         Node var7 = zbvu.a(var1.getChildNodes(), "LicenseExpiry");
         if (var7 != null) {
            this.f = c(var7.getLastChild());
         }

         Node var8 = zbvu.a(var1.getChildNodes(), "OemPublicKeyToken");
         if (this.g != null) {
            this.g = b(var8.getLastChild());
         } else {
            this.g = "";
         }

      } catch (CellsException var9) {
         throw var9;
      } catch (Exception var10) {
         throw new CellsException(9, "The license is invalid. ", var10);
      }
   }

   private static boolean a(Node var0, Node var1) {
      String var2 = var0 != null ? zbvu.a(var0) : "";
      if (var2.equals("<Data />")) {
         var2 = "<Data></Data>";
      } else {
         var2 = var2.replaceAll("&", "&amp;");
      }

      PublicKey var3 = null;

      KeyFactory var4;
      RSAPublicKeySpec var5;
      try {
         var4 = KeyFactory.getInstance("RSA");
         var5 = new RSAPublicKeySpec(new BigInteger(1, zci.a("0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=", "UTF-8")), new BigInteger(1, zci.a("AQAB", "UTF-8")));
         var3 = var4.generatePublic(var5);
      } catch (Exception var9) {
         return false;
      }

      var4 = null;
      var5 = null;

      try {
         byte[] var6 = var2.getBytes("UTF-16LE");
         String var7 = var1 != null ? var1.getLastChild().getNodeValue() : "";
         byte[] var11 = zci.a(var7, "UTF-8");
         Signature var10 = Signature.getInstance("SHA1withRSA");
         var10.initVerify(var3);
         var10.update(var6);
         return var10.verify(var11);
      } catch (Exception var8) {
         return false;
      }
   }

   private static boolean f(String var0) {
      ArrayList var1 = c();
      if (var1 == null) {
         return false;
      } else {
         for(int var2 = 0; var2 < var1.size(); ++var2) {
            if (((String)var1.get(var2)).equals(var0)) {
               throw new CellsException(9, "This license is disabled, please contact Aspose to obtain a new license.");
            }
         }

         return true;
      }
   }

   private static ArrayList c() {
      try {
         InputStream var0 = License.class.getResourceAsStream("/" + License.class.getPackage().getName().replace('.', '/') + "/resources/Aspose.License.BlackList.xml");
         DocumentBuilderFactory var1 = DocumentBuilderFactory.newInstance();
         DocumentBuilder var2 = var1.newDocumentBuilder();
         Document var3 = var2.parse(var0);
         zbvu.a(var3);
         Element var4 = var3.getDocumentElement();
         Node var5 = var4.getElementsByTagName("Data").item(0);
         Node var6 = var4.getElementsByTagName("Signature").item(0);
         if (var5 != null && var6 != null && a(var5, var6)) {
            ArrayList var7 = new ArrayList();
            NodeList var8 = var5.getChildNodes();
            if (var8 != null) {
               for(int var9 = 0; var9 < var8.getLength(); ++var9) {
                  if (var8.item(var9).getNodeName().equals("SN")) {
                     String var10 = b(var8.item(var9).getLastChild());
                     var7.add(var10);
                  }
               }
            }

            return var7;
         } else {
            return null;
         }
      } catch (CellsException var11) {
         throw var11;
      } catch (Exception var12) {
         throw new CellsException(9, "Cannot find black listed licenses resource or black list is invalid. Please report this error to Aspose.");
      }
   }

   private static void a(String[] var0) {
      String var1 = "Aspose.Cells for Java";
      if (var1 != null) {
         for(int var2 = 0; var2 < var0.length; ++var2) {
            if (var1.equals(var0[var2])) {
               return;
            }
         }
      }

      throw new CellsException(9, "The license is not valid for this product.");
   }

   private static void a(Date var0) {
      String var1 = "2015-08-18";
      if (var1 != null && var0 != null) {
         SimpleDateFormat var2 = new SimpleDateFormat("yyyy-MM-dd");
         Date var3 = null;

         try {
            var3 = var2.parse(var1);
         } catch (ParseException var5) {
            throw new CellsException(9, "Invalid release date.");
         }

         if (var3.after(var0)) {
            throw new CellsException(9, "The subscription included in this license allows free upgrades until " + var2.format(var0) + ", but this version of the product was released on " + var1 + ". Please renew the subscription or use a previous version of the product.");
         }
      }

   }

   private static void b(Date var0) {
      if (var0 != null && (new Date()).after(var0)) {
         throw new CellsException(9, "The license has expired.");
      }
   }

   private static String b(Node var0) {
      return var0 != null ? var0.getNodeValue() : "";
   }

   private static Date c(Node var0) {
      String var1 = b(var0);
      if (var1 != "") {
         SimpleDateFormat var2 = new SimpleDateFormat("yyyyMMdd");

         try {
            return var2.parse(var1);
         } catch (ParseException var4) {
            throw new IllegalArgumentException("Invalid SubscriptionExpiry date");
         }
      } else {
         return null;
      }
   }

   private static int g(String var0) {
      if (var0.equals("Professional")) {
         return 1;
      } else if (var0.equals("Enterprise")) {
         return 2;
      } else {
         throw new CellsException(9, "Invalid edition type.");
      }
   }
}
